var vm = new Vue({
    el: '#contentContainer',
    data: {
        form: {
            contentId: $('#contentId').val(),
            channelIdList: [],
            title: null,
            titleImg: null,
            description: null,
            author: null,
            origin: null,
            originUrl: null,
            topLevel: '0',
            releaseDate: moment().format('YYYY-MM-DD HH:mm:ss'),
            content: null
        },
        rules: {
            channelIdList: [
                {type: 'array', required: true, message: '请选择栏目', trigger: 'change'}
            ],
            title: [
                {required: true, message: '请输入标题', trigger: 'blur'},
                {min: 0, max: 150, message: '长度不能超过150个字符', trigger: 'blur'}
            ],
            releaseDate: [
                {required: true, message: '请选择发布日期', trigger: 'change'}
            ]
        },
        options: [],
        props: {
            children: 'children',
            label: 'text',
            value: 'id'
        },
        uploadImageUrl: '/uploadImage?businessId=' + $('#contentId').val(),
        uploadFileUrl: '/uploadFile?businessId=' + $('#contentId').val()
    },
    created: function () {
        var $this = this;

        Common.getJson('/cms/channel/getAllChannelList', {},
            function (response) {
                if (!response || response.status != 200)
                    return;

                var data = response.result;

                // 属性配置信息
                var attributes = {
                    // 标识字段名
                    keyField: 'channelId',
                    // 上级标识字段名
                    parentKeyField: 'parentId',
                    // 文本字段名
                    textField: 'channelName',
                    // 根节点标识
                    rootParentId: 0
                };

                $this.options = Common.convertTreeData(data, attributes);

                var parentChannelId = Common.getUrlParam('parentChannelId');
                if (parentChannelId != null && parentChannelId > 0) {
                    $this.form.channelIdList = Common.getTreeParentIdList(data, parentChannelId, 0, attributes);
                }
            });
    },
    mounted: function () {
        var editorConfig = {
            customConfig: '/js/editorConfig.js'
        };

        editorConfig.filebrowserImageUploadUrl = this.uploadImageUrl;

        CKEDITOR.replace("contentEditor", editorConfig);
    },
    methods: {
        submitForm: function (formName) {
            var $this = this;

            $this.$refs[formName].validate(function (valid) {
                if (!valid)
                    return false;

                var content = CKEDITOR.instances['contentEditor'].getData();
                $this.form.content = content.length > 0 ? content : null;

                Common.postJson('/cms/content/add', $this.form,
                    function (response) {
                        if (!response)
                            return;

                        //保存成功
                        if (response.status == 200 && response.result.success)
                            window.location.href = '/cms/content/list';
                        else {
                            var errMsg = response.status == 200 ? response.result.errMsg : response.msg;

                            $this.$notify.error({
                                title: '错误',
                                message: errMsg,
                                offset: $("#main", window.parent.document).height() - 120,
                                duration: 0
                            });
                        }
                    });
            });
        },
        resetForm: function (formName) {
            this.$refs[formName].resetFields();
        },
        uploadTitleImgSuccess(res) {
            if (res && res.url)
                this.form.titleImg = res.url;
        },
        onAttachmentRemove(file) {
            var $this = this;

            Common.postJson('/deleteAttachment',
                {contentId: $this.form.contentId, fileId: file.response.fileId},
                function (response) {
                    if (!response)
                        return;

                    //删除附件失败
                    if (response.status != 200 || !response.result.success) {
                        $this.$notify.error({
                            title: '错误',
                            message: "删除附件失败",
                            offset: $("#main", window.parent.document).height() - 120,
                            duration: 0
                        });
                    }
                });
        }
    }
});